
#-------------------------------------------------------------------------------
`
Run this function to compute the simulations of the model and to create all the tables and figures.
`
#-------------------------------------------------------------------------------

#Define path/file name to load Arg data
global Arg_Data_path = "../../../Empirics/00_data/02_output/";
#Load functions for simulations
include("../../01_Baseline_Model/01_Model_Structure.jl")
include("../../01_Baseline_Model/02_Functions.jl")
include("../../01_Baseline_Model/10_Simulations_Main_fx.jl")
include("../../01_Baseline_Model/11_Simulations_Moments_Unconditional.jl")
include("../../01_Baseline_Model/12_Simulations_Wrapper.jl")
using Interpolations, Optim, Roots, JLD, Plots
using Distributions, StatsBase

#Create objects to fill in
target_mms, untgt_mms, piBE_mms, RP_mms, Moments_Ctype  = [],[],[],[],[];
name_file = [];

for persistence_val = 1:2

if persistence_val==1
    NQup = 16.0
elseif persistence_val==2
    NQup = 48.0
end
name_file = string("CE_model_structure_Pers_",NQup,".jld")

#Load economy
ce = load(string("model_data/",name_file), "ce");
#Run simulation
target_mms, untgt_mms, piBE_mms, RP_mms, Moments_Ctype    = Simulations_Wrapper(ce; T=500_000, Initial_Type=1, do_plots=false, WINDOWS=1, Compute_η=1);
#------------------------------------------------------------------------------
#------------------------------------------------------------------------------

#-------------------------------------------------------------------------------
#-------------------------------------------------------------------------------
# Import Argentine data; moments
#-------------------------------------------------------------------------------
# sd(C)/sd(Y); c(C,Y); sd(TB/Y)/sd(Y); c(TB/Y,Y); E[SP]; sd(SP); c(SP,Y); E[def]; E[b/Y]
#    1           2          3              4        5       6       7       8       9
data_mms  = readdlm(string(Arg_Data_path,"targets_out.txt"), ',', Float64)
# E[π]; sd(π); c(Δπ,ΔlnSP); c(Δπ,ΔBE); c(π,Y)
#  1     2          3          4         5
data_piBE   = readdlm(string(Arg_Data_path,"piBE_out.txt"), ',', Float64)
elast_data  = readdlm(string(Arg_Data_path,"Estimator_HAT_IV.txt"), ',', Float64)[1]

## Create a vector for data moments
target_dms = [data_mms[9]*0.7; data_mms[8]*100; data_mms[5];     data_mms[6];      27;               elast_data]
piBE_dms   = [data_piBE[1];    data_piBE[2];    0.290;           data_piBE[3]*100; data_piBE[4]*100; data_piBE[5]*100]

#-------------------------------------------------------------------------------
# Store results in .txt files to create .tex tables
#-------------------------------------------------------------------------------
    if persistence_val==1
        str_tgt  = "simulated_moments/target_moments_LP.txt"
        str_piBE = "simulated_moments/piBE_moments_LP.txt"
        str_SPdc = "simulated_moments/SPdecomp_moments_LP.txt"
    elseif persistence_val==2
        str_tgt  = "simulated_moments/target_moments_HP.txt"
        str_piBE = "simulated_moments/piBE_moments_HP.txt"
        str_SPdc = "simulated_moments/SPdecomp_moments_HP.txt"
    end
     #------------------------------------------
     #Targeted moments
     open(str_tgt, "w") do io
     writedlm(io, [target_dms target_mms], ',')
     end
     #Moments for the BE and Misreports
     open(str_piBE, "w") do io
     writedlm(io, [piBE_dms piBE_mms], ',')
     end
     #Spread decomposition (RP moments) [main table]
     open(str_SPdc, "w") do io
     writedlm(io, RP_mms, ',')
     end
#------------------------------------------------------------------------------
#------------------------------------------------------------------------------


end  #persistance_val iteration {1,2}
#------------------------------------------------------------------------------


#----------------------------------------------------------------------------------
#----------------------------------------------------------------------------------
#Appendix Table
#----------------------------------------------------------------------------------
#----------------------------------------------------------------------------------

# Upload simulated moments
#----------------------------------------------------------------------------------
#LOW PERSISTENCE CASE
target_mms_LP 	= readdlm("simulated_moments/target_moments_LP.txt", ',', Float64)    	# E[b/y]; E[def]; E[SP]; sd(SP); B/(B+b); Elast
piBE_mms_LP 	= readdlm("simulated_moments/piBE_moments_LP.txt", ',', Float64) 		# E[π]; sd(π); sd(dBE); c(dπ,dlnSP); c(dπ,dBE); c(π,Y)
SPdcmp_LP 		= readdlm("simulated_moments/SPdecomp_moments_LP.txt", ',', Float64) 	# E[RP]; E[RP/SP]; E[RP/SP|Y<1]; sd(RP); c(RP,Y); c(RP/SP,Y);
#HIGH PERSISTANCE CASE
target_mms_HP 	= readdlm("simulated_moments/target_moments_HP.txt", ',', Float64)    	# E[b/y]; E[def]; E[SP]; sd(SP); B/(B+b); Elast
piBE_mms_HP 	= readdlm("simulated_moments/piBE_moments_HP.txt", ',', Float64) 		# E[π]; sd(π); sd(dBE); c(dπ,dlnSP); c(dπ,dBE); c(π,Y)
SPdcmp_HP 		= readdlm("simulated_moments/SPdecomp_moments_HP.txt", ',', Float64) 	# E[RP]; E[RP/SP]; E[RP/SP|Y<1]; sd(RP); c(RP,Y); c(RP/SP,Y);
#BASELINE MODEL
target_mms_bs 	= readdlm("../../01_Baseline_Model/simulated_moments/target_moments.txt", ',', Float64) 	# E[b/y]; E[def]; E[SP]; sd(SP); B/(B+b); Elast
piBE_mms_bs 	= readdlm("../../01_Baseline_Model/simulated_moments/piBE_moments.txt", ',', Float64) 		# E[π]; sd(π); sd(dBE); c(dπ,dlnSP); c(dπ,dBE); c(π,Y)
SPdcmp_bs 		= readdlm("../../01_Baseline_Model/simulated_moments/SPdecomp_moments.txt", ',', Float64) 	# E[RP]; E[RP/SP]; E[RP/SP|Y<1]; sd(RP); c(RP,Y); c(RP/SP,Y);


#Create Table: Spread Decomposition
#----------------------------------------------------------------------------------
open("tables/spread_decomp.txt", "w") do io
write(io, "\\begin{table}[H] \n")
write(io, "\\centering \n")
write(io, "\\begin{tabular}{llccc} \n")
write(io, "\\hline \\hline \n")
write(io, " \\multirow{2}{*}{Moment} & \\multirow{2}{*}{Description} & \\multicolumn{3}{c}{Persistence} \\\\ \n")
write(io, "  &  & Low & Baseline & High \\\\ \n")
write(io, "\\hline \n")
write(io, "\\ \\ \$\\mathbb{E}[\\Upsilon]\$											& Average reputation premium 			       & \$$(round(Int, SPdcmp_LP[1]))\$bp  & \$$(round(Int, SPdcmp_bs[1]))\$bp  & \$$(round(Int, SPdcmp_HP[1]))\$bp  \\\\ \n")
write(io, "\\ \\ \$\\mathbb{E}[\\Upsilon/SP]\$										& Incidence reputation premium on spreads      & \$$(round(Int, SPdcmp_LP[2]))\\%\$ & \$$(round(Int, SPdcmp_bs[2]))\\%\$ & \$$(round(Int, SPdcmp_HP[2]))\\%\$ \\\\ \n")
write(io, "\\ \\ \$\\sigma(\\Upsilon)/\\sigma(SP)\$									& Reputation premium volatility     	   	   & \$$(round(Int, SPdcmp_LP[6]))\\%\$ & \$$(round(Int, SPdcmp_bs[6]))\\%\$ & \$$(round(Int, SPdcmp_HP[6]))\\%\$ \\\\ \n")
write(io, "\\ \\ \$\\sigma(SP|\\zeta_H)/\\sigma(SP)\$							    & Spread volatility under high reputation      & \$$(round(Int, SPdcmp_LP[7]))\\%\$ & \$$(round(Int, SPdcmp_bs[7]))\\%\$ & \$$(round(Int, SPdcmp_HP[7]))\\%\$ \\\\ \n")
write(io, "\\hline \n")
write(io, "\\ \\ \$\\mathbb{E}[\\Upsilon/SP|Y<Y_l]\$								& Incidence with low output 			       & \$$(round(Int, SPdcmp_LP[3]))\\%\$ & \$$(round(Int, SPdcmp_bs[3]))\\%\$ & \$$(round(Int, SPdcmp_HP[3]))\\%\$ \\\\ \n")
write(io, "\\ \\ \$\\text{corr}(\\Upsilon,\\text{log}Y)\$							& Correlation reputation premium \\& output    & \$$(round(Int, SPdcmp_LP[4]))\\%\$ & \$$(round(Int, SPdcmp_bs[4]))\\%\$ & \$$(round(Int, SPdcmp_HP[4]))\\%\$ \\\\ \n")
write(io, "\\ \\ \$\\text{corr}(\\Upsilon/SP,\\text{log}Y)\$						& Correlation reputation incidence \\& output  & \$$(round(Int, SPdcmp_LP[5]))\\%\$ & \$$(round(Int, SPdcmp_bs[5]))\\%\$ & \$$(round(Int, SPdcmp_HP[5]))\\%\$ \\\\ \n")
write(io, "\\hline \\hline \n")
write(io, "\\end{tabular} \n")
write(io, "\\end{table} \n")
end

